<?php

// 自定义session引擎类
class FileHandler implements SessionHandlerInterface
{
    // 存储路径
    protected $path;

    // 过期时间
    protected $maxlifetime;

    public function __construct($path = 'session', $maxlifetime = 1400)
    {
        $this->path = $this->makeDir($path);
        // echo $this->path;
        $this->maxlifetime = $maxlifetime;
    }

    public function close()
    {
        return true;
    }

    public function destroy($session_id)
    {
        if (is_file($this->path.'/'.$session_id)) {
            // 删除文件
            @unlink($this->path.'/'.$session_id);
        }
        return true;
    }

    public function gc($maxlifetime)
    {
        foreach (glob($this->path.'/*') as $file) {
            if (filemtime($file) + $this->$maxlifetime < time()) {
                @unlink($file);
            }
        }
        return true;
    }

    protected function makeDir($path) {
        is_dir($path) or mkdir($path, 0775, true);
        return realpath($path);
    }

    public function open($save_path, $name)
    {
        return true;
    }

    public function read($session_id)
    {
        return (string) @file_get_contents($this->path.'/'.$session_id);
    }

    public function write($session_id, $session_data)
    {
        return (bool) @file_put_contents($this->path.'/'.$session_id, $session_data);
    }
}